﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HFK.DxFluentUI.DataProvider;
using DevExpress.XtraGrid.Views.Grid;

namespace HFK.DxFluentUI.HorizontalGrid.ComponentModel
{
  public class HorizontalGridView : GridView
  {
    #region internal members

    internal void RegisterRelatedView(string viewName, DataProviderList dataProvider)
    {
      if (!IsMasterDetailEnabled)
      {
        this.MasterRowGetRelationCount += new MasterRowGetRelationCountEventHandler(HorizontalGridView_MasterRowGetRelationCount);
        this.MasterRowGetRelationName += new MasterRowGetRelationNameEventHandler(HorizontalGridView_MasterRowGetRelationName);  
        this.MasterRowGetChildList += new MasterRowGetChildListEventHandler(HorizontalGridView_MasterRowGetChildList);
        this.MasterRowEmpty += new MasterRowEmptyEventHandler(HorizontalGridView_MasterRowEmpty);

        m_relatedViewList = new List<string>();

        IsMasterDetailEnabled = true;
      }

      m_relatedViewList.Add(viewName);
      m_dataProvider = dataProvider;
    }

    #endregion

    #region private members

    List<string> m_relatedViewList;
    DataProviderList m_dataProvider;

    bool IsMasterDetailEnabled { get; set; }

    bool DxMasterRowEmpty(int relationIndex, object parentObject)
    {
      return false;
    }

    int DxMasterRowGetRelationCount()
    {
      return m_relatedViewList.Count;
    }

    string DxMasterRowGetRelationName(int relationIndex, object parentObject)
    {
      return m_relatedViewList[relationIndex];
    }

    IList DxMasterRowGetChildList(int relationIndex, object parentObject)
    {
      return m_dataProvider.ExecuteCallOnObject(m_relatedViewList[relationIndex], parentObject) as IList;
    }

    #region Devexpress abstractions

    static object GetObjectFromRowhandle(GridView gridView, int rowHandle)
    {
      return gridView.GetRow(rowHandle);
    }

    #endregion

    #region Devexpress events

    void HorizontalGridView_MasterRowEmpty(object sender, MasterRowEmptyEventArgs e)
    {
      e.IsEmpty = DxMasterRowEmpty(e.RelationIndex, GetObjectFromRowhandle(sender as GridView, e.RowHandle));
    }

    void HorizontalGridView_MasterRowGetRelationCount(object sender, MasterRowGetRelationCountEventArgs e)
    {
      e.RelationCount = DxMasterRowGetRelationCount();
    }

    void HorizontalGridView_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
    {
      e.ChildList = DxMasterRowGetChildList(e.RelationIndex, GetObjectFromRowhandle(sender as GridView, e.RowHandle));
    }

    void HorizontalGridView_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
    {
      e.RelationName = DxMasterRowGetRelationName(e.RelationIndex, GetObjectFromRowhandle(sender as GridView, e.RowHandle));
    }

    #endregion

    #endregion
  }
}
